package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;


@Service
public class SettingsService {
    @Reference
    private SettingsApi settingsApi;
    @Reference
    private QuestionApi questionApi;

    @Reference
    private BlackListApi blackListApi;

    /**
     * 通知设置读取
     */
    public ResponseEntity<Object> querySettings() {
        //1.获取用户信息
        User user = UserHolder.get();
        // 2.根据用户id查询陌生人问题
        Question question = questionApi.findByUserId(user.getId());
        // 3.根据用户id查询通用设置通知
        Settings settings = settingsApi.findByUserId(user.getId());
        // 4.封装返回结果对象
        SettingsVo settingsVo = new SettingsVo();
        // 4.1 封装陌生人问题
        if (question != null) {
            settingsVo.setStrangerQuestion(question.getTxt());
        }
        // 4.2 封装通用设置通知
        if (settings != null) {
            BeanUtils.copyProperties(settings, settingsVo);
        }
        // 4.3 封装手机号码
        settingsVo.setPhone(user.getMobile());
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置用户通知保存
     *
     * @param settings 用户提交的设置通知对象
     * @return
     */
    public ResponseEntity<Object> saveNotification(Settings settings) {
        //1. 获取登陆用户
        Long userId = UserHolder.getUserId();
        //2. 根据用户id查找该用户的通知设置
        Settings dbSettings = settingsApi.findByUserId(userId);
        //3. 如果没有该用户的通知设置，代表该用户是第一次设置，需要做保存动作
        if (dbSettings == null) {
            settings.setUserId(userId);
            settingsApi.save(settings);
        } else {
            settings.setId(dbSettings.getId());
            //4. 如果有该用户的通知设置，那么该用户就是做更新操作
            settingsApi.update(settings);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 接口名称：设置陌生人问题 - 保存
     */
    public ResponseEntity<Object> saveQuestion(String content) {
        // 1.从当前线程上获取用户信息
        Long userId = UserHolder.getUserId();
        // 2.根据用户id，查询陌生人问题
        Question question = questionApi.findByUserId(userId);
        // 3.判断
        if (question == null) {
            // 3.1 如果陌生人问题为空，则添加
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            // 3.2 如果陌生人问题不为空，则修改
            question.setTxt(content);
            questionApi.update(question);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 接口名称：黑名单 - 翻页列表
     */
    public ResponseEntity<Object> blacklist(Integer page, Integer pagesize) {
        // 1.获取用户id
        Long userId = UserHolder.getUserId();
        // 2.调用Api服务分页查询
        IPage<UserInfo> iPage = blackListApi.findBlackList(page, pagesize, userId);
        // 3.封装返回结果：PageResult
        PageResult pageResult = new PageResult(
                page, pagesize, (int) iPage.getTotal(), iPage.getRecords()
        );
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 接口名称：黑名单 - 移除
     */
    public ResponseEntity<Object> deleteBlackUser(Long blackUserId) {
        // 获取登陆用户id
        Long userId = UserHolder.getUserId();
        // 调用api服务，移除黑名单用户(从tb_black_list表中删除一条记录)
        blackListApi.deleteBlackUser(userId, blackUserId);
        return ResponseEntity.ok(null);
    }
}
